package com.ly.erms.modular.erms.controller;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.erms.common.annotion.BussinessLog;
import com.ly.erms.common.annotion.Permission;
import com.ly.erms.common.constant.dicmap.DatarecordDict;
import com.ly.erms.common.constant.factory.PageFactory;
import com.ly.erms.common.exception.BizExceptionEnum;
import com.ly.erms.common.persistence.dao.PubliclibraryDatarecordMapper;
import com.ly.erms.common.persistence.model.PubliclibraryDatarecord;
import com.ly.erms.config.properties.ErmsProperties;
import com.ly.erms.core.base.controller.BaseController;
import com.ly.erms.core.exception.ErmsException;
import com.ly.erms.core.util.IdGenerator;
import com.ly.erms.modular.erms.dao.DatarecordDao;
import com.ly.erms.modular.erms.service.IDatarecordService;
import com.ly.erms.modular.erms.warpper.DatarecordWarpper;


/**
 * 文件主表 控制器
 * 
 * @author liuyang
 * @date 2018年1月15日
 */
@Controller
@RequestMapping("/publiclibrary")
public class DaterecordController extends BaseController {
	private String PREFIX = "/erms/publiclibrary/";

	@Resource
	PubliclibraryDatarecordMapper datarecordMapper;

	@Resource
	DatarecordDao datarecordDao;
	
	@Resource
	IDatarecordService datarecordService;

	@Resource
	ErmsProperties ermsProperties;

	@RequestMapping("")
	public String index() {
		return PREFIX + "/publiclibrary.html";
	}
	
	/**
	 * 跳转到上传页面
	 * @return
	 */
	@RequestMapping("/publiclibrary_upload/{classificationId}")
	public String toUpload(@PathVariable Integer classificationId,Model model) {
		model.addAttribute("classificationId", classificationId);
		return PREFIX + "/publiclibrary_upload.html";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/list")
	@Permission
	@ResponseBody
	public Object list(@RequestParam(required = false) Integer pid, @RequestParam(required = false) String name,
			@RequestParam(required = false) String begintime, @RequestParam(required = false) String endtime) {
		Page<PubliclibraryDatarecord> page = new PageFactory<PubliclibraryDatarecord>().defaultPage();
		List<Map<String, Object>> daterecordList = datarecordDao.list(page,pid, name, begintime, endtime);
		page.setRecords((List<PubliclibraryDatarecord>) new DatarecordWarpper(daterecordList).warp());
		return super.packForBT(page);
	}

	/**
	 * 文件上传
	 */
	@BussinessLog(value = "上传文件",key="name",dict = DatarecordDict.class)
	@RequestMapping(path = "/upload/{classificationId}", method = RequestMethod.POST)
	@ResponseBody
	public Object upload(@RequestPart("file") MultipartFile file,@PathVariable Integer classificationId) {
		String savePath = ermsProperties.getPublicFilePath();
		try {
				String originName = file.getOriginalFilename();
				String newName = IdGenerator.getId()+originName.substring(originName.lastIndexOf("."));
				String filePath = savePath + newName;
				file.transferTo(new File(filePath));
				//文件入库
				datarecordService.AddInDatarecord(file,classificationId,filePath);
		} catch (Exception e) {
			throw new ErmsException(BizExceptionEnum.UPLOAD_ERROR);
		}
		return SUCCESS_TIP;
	}
	
	/**
	 * 在线浏览
	 */
	@RequestMapping("/view/{fileId}")
	public String view(@PathVariable Integer fileId,HttpServletRequest request, HttpServletResponse response){
		
        return "/viewObject.jsp";
	}
}
